/*
 * This file is part of "Deathwatch Ammo Counter for Android".
 *
 * "Deathwatch Ammo Counter for Android" is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 *
 * "Deathwatch Ammo Counter for Android" is distributed in the hope that it 
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with "Deathwatch Ammo Counter for Android".  
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package com.TikiiTorch.Deathwatch.AmmoCounter.activities;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

import com.TikiiTorch.Deathwatch.AmmoCounter.C;
import com.TikiiTorch.Deathwatch.AmmoCounter.R;
import com.TikiiTorch.Deathwatch.AmmoCounter.Weapon;
import com.TikiiTorch.Deathwatch.AmmoCounter.file.FileManager;
import com.TikiiTorch.Deathwatch.AmmoCounter.file.WeaponHandler;

public class WeaponPicker extends Activity {

	public static final int PICK_WEAPON_REQUEST = 0x11;

	ListView list;

	String gunType;

	WeaponListAdapter adapter;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.weapon_list);

		ExpandableListView list1 = (ExpandableListView) findViewById(R.id.weaponListView);

		String[] groups = WeaponHandler.getWeaponGroups(this);

		Weapon[][] children = null;
		try {
			children = FileManager.getWeaponList(this);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		adapter = new WeaponListAdapter(groups, children, this);

		list1.setAdapter(adapter);

		// set the expander icon to the right
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int width = metrics.widthPixels;
		list1.setIndicatorBounds(width - getDipsFromPixel(50), width
				- getDipsFromPixel(10));

		list1.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Weapon weapon = (Weapon) adapter.getChild(groupPosition,
						childPosition);

				Intent returnIntent = new Intent();

				returnIntent.putExtra("selectedWeapon", weapon);

				setResult(RESULT_OK, returnIntent);

				finish();

				return false;
			}
		});

	}

	public int getDipsFromPixel(float pixels) {
		// get the screen's density scale
		final float scale = getResources().getDisplayMetrics().density;

		// convert the dps to pixels based on density scale
		return (int) (pixels * scale + 0.5f);
	}

	public TextView getGenericView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT, 64);

		TextView textView = new TextView(WeaponPicker.this);
		textView.setLayoutParams(lp);
		// Center the text vertically
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		// Set the text starting position
		textView.setPadding(56, 0, 0, 0);
		return textView;
	}

	private class WeaponListAdapter extends BaseExpandableListAdapter {

		Context context;

		private String[] groups;
		private Weapon[][] children;

		public WeaponListAdapter(String[] groups, Weapon[][] children,
				Context context) {
			this.groups = groups;
			this.children = children;
			Log.d(C.TAG, "groups size: " + groups.length);
			Log.d(C.TAG, "children[x] size: " + children.length);
			Log.d(C.TAG, "children[1] size: " + getChildrenCount(0));

			this.context = context;
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return children[groupPosition][childPosition];
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {

			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

				convertView = inflater.inflate(R.layout.weapon_group_row, null);
			}

			TextView tvGroupname = (TextView) convertView
					.findViewById(R.id.tvGroupName);
			tvGroupname.setText(getGroup(groupPosition).toString());

			return convertView;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

				convertView = inflater.inflate(R.layout.weapon_child_row, null);
			}

			TextView tvGroupname = (TextView) convertView
					.findViewById(R.id.tvChildName);
			tvGroupname.setText(getChild(groupPosition, childPosition).toString());

			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return children[groupPosition].length;
		}

		@Override
		public Object getGroup(int groupPosition) {
			return groups[groupPosition];
		}

		@Override
		public int getGroupCount() {
			return groups.length;
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

	}

}
